<?php 
class annonce_gerer extends MX_Controller{
	public $category;
	public function modifier($id=0){
		
		$anonnceInfo= $this->load->Module('product/show');		
		$annonce=$anonnceInfo->get_info($id);

		$loadTarif =$this->load->Module('setting/tarif');
		$tarif=$loadTarif->get_tarif();

		if(!$annonce){
				
			$data=array(
					'title'=>"Cette annonce est désactivée",
					'description'=>"Annonce non-trouvable",
					'keywords'=>'',
			);
			$data=$setting->data($data);
		
			$this->view(
					array('header',
							'annonce-desactive',
							'footer'),
									$data); // Display the page
							return false;
		}

		if(isset($annonce->option)) {
			$option=$this->load->module('setting/option');
			$annonce->option=$option->getOptions($annonce->category_id,$annonce->option,true);
		}
		
		$data=array(

				'title'=>"Modifier l'annonce ".$annonce->prd_name,
				'description'=>"vous modifiez l'annonce ".$annonce->prd_name,
				'keywords'=>'',
				'tarif'=>$tarif,		
				'filter'=>'',
				'annonce'=>$annonce
		);
		$setting=$this->load->Module('setting/data');
		$data=$setting->data($data);
		
		$this->view(
				array('header',
					'annonce-modifier',
					'annonce-deposer-footer'),
				$data); // Display the page
	}
	public function supprimer($id=0){
	
		$setting=$this->load->Module('setting/data');
		$annonceInfo= $this->load->Module('product/show');
		$annonce=$annonceInfo->get_info($id);
	
		$loadTarif =$this->load->Module('setting/tarif');
		$tarif=$loadTarif->get_tarif();
	
		if(!$annonce){
			
			$data=array(
					'title'=>"Cette annonce est désactivée",
					'description'=>"Annonce non-trouvable",
					'keywords'=>'',
			);
		$data=$setting->data($data);
		
			$this->view(
					array('header',
							'annonce-desactive',
							'footer'),
							$data); // Display the page
					return false;
		}
	
		if(isset($annonce->option)) {
			$option=$this->load->module('setting/option');
			$annonce->option=$option->getOptions($annonce->category_id,$annonce->option,true);
		}
	
		$data=array(
				'title'=>"Supprimer l'annonce",
				'description'=>"vous supprimez l'annonce ".$annonce->prd_name,
				'keywords'=>'',		
				'filter'=>'',
				'annonce'=>$annonce
		);
		$data=$setting->data($data);
		$this->view(
				array('header',
						'annonce-supprimer',
						'annonce-info',
						'annonce-deposer-footer'),
					$data); // Display the page
	}
	public function envoyer_a_ami($id=0){

		$setting=$this->load->Module('setting/data');
		$anonnceInfo= $this->load->Module('product/show');
		$annonce=$anonnceInfo->get_info($id);
		
		$loadTarif =$this->load->Module('setting/tarif');
		$tarif=$loadTarif->get_tarif();
		if(!$annonce){
			$data=array(
					'title'=>"Cette annonce est désactivée",
					'description'=>"Annonce non-trouvable",
					'keywords'=>''
			);
			$data=$setting->data($data);
		
			$this->view(
					array('header',
							'annonce-desactive',
							'footer'),
							$data); // Display the page
					return false;
		}
		
		$option=$this->load->module('setting/option');
		$annonce->option=$option->getOptions($annonce->category_id,$annonce->option,true);
		
		$data=array(
				'title'=>"Conseiller l'annonce à un ami",
				'description'=>"Conseiller l'annonce à un ami ".$annonce->prd_name,
				'keywords'=>"Conseiller annonce ami, envoyer annonce ami  ".$annonce->prd_name,
				'annonce'=>$annonce,
				'filter'=>''
		);
		
		$data=$setting->data($data);
		
		$this->view(
				array('header',
						'annonce-envoyer-ami',
						'annonce-info',
								'annonce-deposer-footer'),
				$data); // Display the page
	}
	public function envoyer_annonce(){

		$setting=$this->load->Module('setting/data');
		$anonnceInfo= $this->load->Module('product/show');
		$annonce=$anonnceInfo->get_info($this->input->post('id'));
		$loadTarif =$this->load->Module('setting/tarif');
		$tarif=$loadTarif->get_tarif();
		if(!$annonce){
			$data=array(
					'title'=>"Cette annonce est désactivée",
					'description'=>"Annonce non-trouvable",
					'keywords'=>''
			);
			
			$data=$setting->data($data);
			$this->view(
					array('header',
							'annonce-desactive',
							'footer'),
								$data); // Display the page
						return false;
			}
			
			$option=$this->load->module('setting/option');
			$annonce->option=$option->getOptions($annonce->category_id,$annonce->option,true);
			//send email
			$email= $this->load->Module('send/email');
			//$settings=$this->load->Module('setting/admin');
			$edition=$this->load->Module('setting/edition');
			//$setting_admin=$settings->get_admin();
			$edition_send=$edition->get_Edition('envoyer_annonce_a_ami');
			$from_email=$this->input->post('fromemail');
			$from_name='Caverne.fr';
			$to_email=$this->input->post('sendMail');
			$subject="";
			$message=$edition_send[0]->setting;
			$message_data=(array)$annonce;
			$message_data['from_email']=$from_email;
			$ami=explode('@',$from_email);
			$message_data['from_name']=isset($ami[0])?$ami[0]:$from_name;
			foreach ($message_data as $key=>$data){
				if(is_string ($data)) $message=str_replace('{'.$key.'}',$data,$message);
			}
			$status='send';
			$action='send_to_friend';
			$description='';
			
			$email->add($from_email,$from_name,$to_email,$subject,$message,$status,$action,$description);
			
			$data=array(
					'title'=>"Conseiller l'annonce à un ami ".$annonce->prd_name,
					'description'=>"vous conseillez à votre ami l'annonce".$annonce->prd_name,
					'keywords'=>"Conseiller annonce ami, envoyer annonce amin",		
					'filter'=>'',
					'annonce'=>$annonce
			);
			
			$data=$setting->data($data);
			$this->view(
					array('header',
							'annonce-envoyer-bon',
							'annonce-info',
							'annonce-deposer-footer'),
					$data); // Display the page
		}

		public function signaler($id=0){

			$setting=$this->load->Module('setting/data');
			$anonnceInfo= $this->load->Module('product/show');
			$annonce=$anonnceInfo->get_info($id);
		
			$loadTarif =$this->load->Module('setting/tarif');
			$tarif=$loadTarif->get_tarif();
			if(!$annonce){
		
				$data=array(
						'title'=>"Cette annonce est désactivée",
						'description'=>"Annonce non-trouvable",
						'keywords'=>''
				);
				$data=$setting->data($data);
				$this->view(
						array('header',
								'annonce-desactive',
								'footer'),
									$data); // Display the page
							return false;
			}
			
			$option=$this->load->module('setting/option');
			$annonce->option=$option->getOptions($annonce->category_id,$annonce->option,true);
			
			$data=array(
					'title'=>"Signaler un contenu abusif ".$annonce->prd_name,
					'description'=>"Merci de nous signaler un contenu abusif de l'annonce".$annonce->prd_name,
					'keywords'=>'Signaler contenu abusif',		
					'filter'=>'',
					'annonce'=>$annonce
			);
			$data=$setting->data($data);
			$this->view(
					array('header',
							'annonce-signaler',
							'annonce-info',
							'annonce-deposer-footer'),
					$data); // Display the page
	}
	public function signaler_confirmer($id=0){

		$setting=$this->load->Module('setting/data');
		if(!$id){
			$id=$this->input->post('id');
		}
		$anonnceInfo= $this->load->Module('product/show');
		$annonce=$anonnceInfo->get_info($id);
		$loadTarif =$this->load->Module('setting/tarif');
		$tarif=$loadTarif->get_tarif();
		if(!$annonce){
	
			$data=array(
					'title'=>"Cette annonce est désactivée",
					'description'=>"Annonce non-trouvable",		
					'filter'=>'',
					'keywords'=>''
			);
			$data=$setting->data($data);
			$this->view(
					array('header',
							'annonce-desactive',
							'footer'),
									$data); // Display the page
			return false;
		}
				
		$option=$this->load->module('setting/option');
		$annonce->option=$option->getOptions($annonce->category_id,$annonce->option,true);
		//send email
		$email= $this->load->Module('send/email');
		$setting=$this->load->Module('setting/admin');
		$edition=$this->load->Module('setting/edition');
		//$setting_admin=$setting->get_admin();
		$edition_send=$edition->get_Edition('envoyer_annonce_signaler');
		$from_email=$this->input->post('sendMail');
		$from_name=$this->input->post('nom');
		$to_email=$this->input->post('sendMail');
		$subject=$this->input->post('motif')." : ".$annonce->prd_name;


		$message=$edition_send[0]->setting;
		$message_data=(array)$annonce;
		$message_data['from_email']=$from_email;
		foreach ($message_data as $key=>$data){
			if(is_string ($data)) $message=str_replace('{'.$key.'}',$data,$message);
		}
		
		$status='send';
		$action='signalation_annonce';
		$description='';
		$message.=$this->input->post('message')." <br/> \n".
		'<a class="btn btn-primary btn-large" href="'.$this->config->config['base_url'].$annonce->link_category.'/'.$annonce->prd_link.'.html">'.$annonce->prd_name.'</a>';
		
		
		
		$email->add($from_email,$from_name,$to_email,$subject,$message,$status,$action,$description);
		
		$data=array(
				'title'=>"Confirmer la Signalisation  d'un contenu abusif",
				'description'=>"Confirmer la Signalisation",
				'keywords'=>'',
				'annonce'=>$annonce
		);
		$data=$setting->data($data);
		$this->view(
				array('header',
						'annonce-envoyer-bon',
						'annonce-info',
								'annonce-deposer-footer'),
				$data); // Display the page
	}	
	public function modifier_annonce(){
		
		$setting=$this->load->Module('setting/data');
		$anonnceInfo= $this->load->Module('product/show');
		$id=$this->input->post('id');
		$annonce=$anonnceInfo->get_info($id);
		$message=array();
		if($annonce->user_password==$this->input->post('password')){
			$message[]="Vos changements ont été enregistrés";
			
			$product=new stdclass;
			$product->prd_id=$this->input->post('id');
			$product->prd_price=str_replace(',','.',$this->input->post('price'));
			$product->prd_name=$this->input->post('tilte');
			$product->prd_description=$this->input->post('description');
			$product->prd_codepostal=$this->input->post('code_postal');
			$product->prd_departement=substr($product->prd_codepostal, 0,2);
			//outre mer
			if($product->prd_departement==97) $product->prd_departement=substr($product->prd_codepostal, 0,3);
			//mis a jour le date de creation 
			$product->date_validate= strtotime("now");
			
			$product->prd_youtube= $this->input->post('youtube');
			$product->user_name= $this->input->post('nom');
			$product->user_tel= $this->input->post('telephone');
			$product->user_tel_show= $this->input->post('user_tel_show');
			$product->option=$this->input->post('option');
			$path=FCPATH."images/";
			$delPhoto=$this->input->post('delPhoto');
			if(!empty($delPhoto)){
				$this->model('annonce_models_creer')->delPhoto(array_keys($delPhoto));
				foreach ($delPhoto as $photo){
					if(file_exists($path.$photo)) unlink($path.$photo);
					if(file_exists($path."thumbnails/".$photo)) unlink($path."thumbnails/".$photo);
				}
			}
			$photos=$this->input->post('photo');
			//deplacer les photo dans bon dossier de categories
			if(!empty($photos))$product->photo=self::copy_Photo(FCPATH."images/",$annonce->category_id,$photos);

			$this->model('product_models_admin')->edit($product);
			$annonce=$anonnceInfo->get_info($id);
		}else{
			$message[]="<h3 class='text-orange'>Votre mot de passe n'est pas valide</h3>";
			$message[]="<a  href='modifier/$id' class='btn btn-primary btn-small'>Retourner à la page Modifier</a>";
		}
		
		
		$data=array(
				'title'=>"Modifier l'annonce ".$annonce->prd_name,
				'description'=>"vous Modifiez l'annonce ".$annonce->prd_name,
				'keywords'=>'',
				'annonce'=>$annonce,		
				'filter'=>'',
				'message'=>$message
		);
		$data=$setting->data($data);
		$this->view(
				array('header',
						'annonce-modifier-confirme',
						'annonce-info',
						'footer'),
						$data); // Display the page
	}
	public function supprimer_annonce(){

		$setting=$this->load->Module('setting/data');
		$anonnceInfo= $this->load->Module('product/show');
		$id=$this->input->post('id');
		$annonce=$anonnceInfo->get_info($id);
		$message=array();
		if($annonce->user_password==$this->input->post('password')){
			$message[]="Votre annonce a été supprimée définitivement";
			$this->load->Module('product/admin');
			$this->product_admin->delete($id);
		}else{
			$message[]="<h3 class='text-orange'>Votre mot de passe n'est pas valide</h3>";
			$message[]="<a href='supprimer/$id' class='btn btn-primary btn-small'>Retourner à la page Supprimer</a>";
		}
	
	
		$data=array(
				'title'=>"Supprimer l'annonce ".$annonce->prd_name,
				'description'=>"Vous confimez la suppression de l'annonce ".$annonce->prd_name,
				'keywords'=>'',
				'annonce'=>$annonce,		
				'filter'=>'',
				'message'=>$message
		);
		$data=$setting->data($data);
		$this->view(
				array('header',
						'annonce-supprimer-confirme',
						'annonce-info',
						'footer'),
							$data); // Display the page
	}

	public function remonter($id=0){

		$setting=$this->load->Module('setting/data');
		$anonnceInfo= $this->load->Module('product/show');		
		$annonce=$anonnceInfo->get_info($id);

		$loadTarif =$this->load->Module('setting/tarif');
		$tarif=$loadTarif->get_tarif();

		if(!$annonce){
			
			$data=array(
					'title'=>"Cette annonce est désactivée",
					'description'=>"Annonce non-trouvable",
					'keywords'=>'',
			);
			$data=$setting->data($data);
			$this->view(
					array('header',
							'annonce-desactive',
							'footer'),
					$data); // Display the page
			return false;
		}

		$option=$this->load->module('setting/option');
		$annonce->option=$option->getOptions($annonce->category_id,$annonce->option,true);
		
		$data=array(
				'title'=>"Remonter l'annonce ".$annonce->prd_name,
				'description'=>"vous voulez remonter l'annonce  ".$annonce->prd_name,
				'keywords'=>'',
				'top'=>1,
				'tarif'=>$tarif,		
				'filter'=>'',
				'annonce'=>$annonce
		);
		
		$setting=$this->load->Module('setting/data');
		$data=$setting->data($data);
		
		$this->view(
				array('header',
					'annonce-tarif',
					'annonce-info',
					'annonce-deposer-footer'),
				$data); // Display the page
	}
	

	public function avant($id=0){

		$setting=$this->load->Module('setting/data');
		$anonnceInfo= $this->load->Module('product/show');
		$annonce=$anonnceInfo->get_info($id);
	
		$loadTarif =$this->load->Module('setting/tarif');
		$tarif=$loadTarif->get_tarif();
	
		if(!$annonce){
				
			$data=array(
					'title'=>"Cette annonce est désactivée",
					'description'=>"Annonce non-trouvable",
					'keywords'=>''
			);
			$data=$setting->data($data);
			$this->view(
					array('header',
							'annonce-desactive',
							'footer'),
						$data); // Display the page
				return false;
		}

		$option=$this->load->module('setting/option');
		$annonce->option=$option->getOptions($annonce->category_id,$annonce->option,true);
		
		$data=array(
				'title'=>"Avancer l'annonce ".$annonce->prd_name,
				'description'=>"Vous voulez avancer l'annonce ".$annonce->prd_name." dans le top de la liste de recherche",
				'keywords'=>'',
				'priority'=>1,
				'tarif'=>$tarif,		
				'filter'=>'',
				'annonce'=>$annonce
		);
		$data=$setting->data($data);
		$this->view(
				array('header',
					'annonce-tarif',
					'annonce-info',
					'annonce-deposer-footer'),
				$data); // Display the page
	}
	public function sauvegarder($id=0){
	
		$setting=$this->load->Module('setting/data');
		$anonnceInfo= $this->load->Module('product/show');
		$annonce=$anonnceInfo->get_info($id);
	
		$loadTarif =$this->load->Module('setting/tarif');
		$tarif=$loadTarif->get_tarif();
		if(!$annonce){
	
			$data=array(
					'title'=>"Cette annonce est désactivée",
					'description'=>"Annonce non-trouvable ",
					'keywords'=>''
			);
			$data=$setting->data($data);
			$this->view(
					array('header',
							'annonce-desactive',
							'footer'),
						$data); // Display the page
								return false;
		}
		
		
		$this->load->helper('cookie');

		$saveAnnonce=$this->input->cookie('annonce');
		if($saveAnnonce=='')$saveAnnonce= $id;
		else {
			$extractId=explode(',',$saveAnnonce);
			$extractId[]=$id;
			$extractId=array_unique($extractId);
			$saveAnnonce=implode(',',$extractId);
		}
		$cookie = array(
				'name'   => 'annonce',
				'value'  => $saveAnnonce,
				'expire' => '15000000',
				'prefix' => ''
		);
		$this->input->set_cookie($cookie);
		
		
		$data=array(
				'title'=>"Sauvegarder l'annonce",
				'description'=>"Sauvegarder l'annonce ".$annonce->prd_name,
				'keywords'=>"Vous sauvegardez l'annonce ".$annonce->prd_name.", retrouvez dans mes annonces" ,		
				'filter'=>'',
				'annonce'=>$annonce
		);
		$data=$setting->data($data);
		$this->view(
				array('header',
						'annonce-sauvegarder',
						'annonce-info',
						'annonce-deposer-footer'),
				$data); // Display the page
	}
	public function payer(){
		$id=$this->input->post('id');
		$setting=$this->load->Module('setting/data');
		$anonnceInfo= $this->load->Module('product/show');
		$annonce=$anonnceInfo->get_info($id);
	
		$loadTarif =$this->load->Module('setting/tarif');
		$tarif=$loadTarif->get_tarif();
		if(!$annonce){
			$data=array(
					'title'=>"Cette annonce est désactivée",
					'description'=>"Annonce non-trouvable",
					'keywords'=>''
			);
			$data=$setting->data($data);
			$this->view(
					array('header',
							'annonce-desactive',
							'footer'),
							$data); // Display the page
									return false;
		}
			
		
		$data=array(
				'title'=>"Confirmer de payment l'annonce ".$annonce->prd_name,
				'description'=>"payer l'annonce",
				'keywords'=>"Vous shouhaitez payer l'annonce ".$annonce->prd_name.", merci de nous faire confiance",		
				'filter'=>'',
				'annonce'=>$annonce
		);
		$data=$setting->data($data);
		$this->view(
				array('header',
						'annonce-payer',
						'annonce-deposer-footer'),
				$data); // Display the page
		}
	public function imprimer($id=0){
		
			$setting=$this->load->Module('setting/data');
			$anonnceInfo= $this->load->Module('product/show');
			$annonce=$anonnceInfo->get_info($id);
		
			$loadTarif =$this->load->Module('setting/tarif');
			$tarif=$loadTarif->get_tarif();
			if(!$annonce){
		
				$data=array(
						'title'=>"Cette annonce est désactivée",
						'description'=>"Annonce non-trouvable",
						'keywords'=>''
				);
				$data=$setting->data($data);
				$this->view(
						array('header',
								'annonce-desactive',
								'footer'),
								$data); // Display the page
										return false;
				}
				
				
				$this->load->helper('cookie');
		
				$saveAnnonce=$this->input->cookie('annonce');
				if($saveAnnonce=='')$saveAnnonce= $id;
				else {
					$extractId=explode(',',$saveAnnonce);
					$extractId[]=$id;
					$extractId=array_unique($extractId);
					$saveAnnonce=implode(',',$extractId);
				}
				
				
				$data=array(
						'title'=>" Imprimez ".$annonce->prd_name,
						'description'=>"Vous imprimez l'annonce ".$annonce->prd_name,
						'keywords'=>"Imprimer annonce ",		
						'filter'=>'',
						'annonce'=>$annonce
				);
				$data=$setting->data($data);
				$this->view(
						array('header-imprimer',
								'annonce-info',
								'footer-imprimer'),
						$data); // Display the page
	}	
	private function copy_Photo($path,$cat,$photos=array()){
		if(!empty($photos))
		foreach ($photos as $photo){
			$temp_photo=$path.$photo;
			$temp_thumb_photo=$path."thumbnails/".$photo;
			$NameCatPhoto=$cat."/$photo";
	
			if(!is_dir($path.$cat))mkdir($path.$cat, 0777);
			if(!is_dir($path."thumbnails/".$cat))mkdir($path."thumbnails/".$cat, 0777);
	
			if (@copy($temp_photo,"$path/$NameCatPhoto")) unlink($temp_photo);
			if (@copy($temp_thumb_photo,$path."thumbnails/$NameCatPhoto")) unlink($temp_thumb_photo);
	
			$product_photo[]=$NameCatPhoto;
		}
		if(isset($product_photo)) return $product_photo;
	
		return false;
	}
	public function ajax_region(){
		$Region='R_'.$this->input->post('departement');
		
		$this->load->driver('cache');
		
		// Save into the cache for 5 minutes
		if ( ! $$Region = $this->cache->file->get($Region))
		{
			$localisation= $this->load->module('localisation/index');
			$localisation->Region="";
			$localisation->departement=$this->input->post('departement');
			$$Region= $localisation->OptionRegion();
			$this->cache->file->save($Region, $$Region, 300);
		}
		echo $$Region;
		
		
	}
	public function ajax_departement(){
		$departement='D_'.$this->input->post('departement');
		
		$this->load->driver('cache');
		
		// Save into the cache for 5 minutes
		if ( ! $$departement = $this->cache->file->get($departement))
		{
			$localisation= $this->load->module('localisation/index');
			$localisation->departement=$this->input->post('departement');
			$$departement= $localisation->OptionDepartement2Code();
			$this->cache->file->save($departement, $$departement, 300);
		}
		echo $$departement;
	}

	public function ajax_ville(){
		$localisation= $this->load->module('localisation/index');
		$localisation->departement=$this->input->post('departement');
		echo $localisation->OptionVille();
	
	}
	public function ajax_check_password(){
		$password=$this->input->post('password');
		$id=$this->input->post('id');
		$anonnceInfo= $this->load->Module('product/show');
		$annonce=$anonnceInfo->get_info($id);
		if($annonce->user_password!=$password){
			echo '<p class="text-orange">Votre mot de passe n\'est pas correct</p> <label for="email">Mot de passe oublié ? </label> <input id="oublie_pw" type="button" value="Envoyer par email" />';
		}
	}
	public function ajax_send_password(){
		$id=$this->input->post('id');
		$anonnceInfo= $this->load->Module('product/show');
		$annonce=$anonnceInfo->get_info($id);
		if(isset($annonce->user_password)){
			$email= $this->load->Module('send/email');
			$setting=$this->load->Module('setting/admin');
			$edition=$this->load->Module('setting/edition');
			$setting_admin=$setting->get_admin();
			$edition_send_password=$edition->get_Edition('send_password');
			$from_email=$setting_admin['admin_email'];
			$from_name='Caverne.fr';
			$to_email=$annonce->user_email;
			$subject="";
			$message=$edition_send_password[0]->setting;
			$message_data=(array)$annonce;
			foreach ($message_data as $key=>$data){
				if(is_string ($data)) $message=str_replace('{'.$key.'}',$data,$message);
			}
			$status='send';
			$action='send_password';
			$description='';
			
			$email->add($from_email,$from_name,$to_email,$subject,$message,$status,$action,$description);
			echo '<p class="text-orange">Votre mot de passe n\'est pas correct</p> <label for="email">Mot de passe oublié ? </label> <input id="oublie_pw" type="button" value="Envoyer par email" />';
		}else{
			echo "votre référence n'est pas correct";
		}
	}
	public function active($link){
		$id=$this->model('product_models_show')->active_id_from_link($link);
		
		if($id){
			$anonnceInfo= $this->load->Module('product/show');
			$annonce=$anonnceInfo->get_info($id);
			$this->load->helper('url');
			$_url=$annonce->link_category.'/'.$annonce->prd_link.'.html';
			redirect($_url, 'refresh');
		}
		$data=array(
				'title'=>"Cette annonce est désactivée",
				'description'=>"Annonce non-trouvable",
				'keywords'=>''
		);
		$setting=$this->load->Module('setting/data');
		$data=$setting->data($data);
		$this->view(
				array('header',
						'annonce-desactive',
						'footer'),
				$data); // Display the page
	}
}